Code That Fits in Your Head: Heuristics for Software Engineering by Mark Seemann
Author:Mark Seemann [Mark Seemann]
Language: eng
Format: epub
Publisher: Addison-Wesley Professional
Published: 2021-07-08T16:00:00+00:00
8.2.3 Implementation details
Itâs good to know that you can use a class like MaitreD without knowing all of the implementation details. Sometimes, however, your task involves changing the behaviour of an object. When thatâs your task, youâll need to go a level deeper in the fractal architecture. Youâll have to read the code.
Listing 8.13 shows the WillAccept implementation. It stays within the bounds of humane code. Its cyclomatic complexity is 5, it has twenty lines of code, stays within 80 characters in width, and activates seven objects.
public bool WillAccept( DateTime now, IEnumerable<Reservation> existingReservations, Reservation candidate) { if (existingReservations is null) throw new ArgumentNullException(nameof(existingReservations)); if (candidate is null) throw new ArgumentNullException(nameof(candidate)); if (candidate.At < now) return false; if (IsOutsideOfOpeningHours(candidate)) return false; var seating = new Seating(SeatingDuration, candidate); var relevantReservations = existingReservations.Where(seating.Overlaps); var availableTables = Allocate(relevantReservations); return availableTables.Any(t => t.Fits(candidate.Quantity)); }
Listing 8.13: The WillAccept method. (Restaurant/62f3a56/Restaurant.RestApi/MaitreD.cs)
Itâs not the whole implementation. The way to stay within the bounds of code that fits in your brain is to aggressively delegate pieces of the implementation to other parts. Take a moment to look at the code and see if you get the gist of it.
Youâve never seen the Seating class before. You donât know what the Fits method does. Still, hopefully you can get a sense of where to look next, depending on your motivation for looking at the code. If you need to change the way the method allocates tables, where would you look? If thereâs a bug in the seating overlap detection, where do you go next?
You could decide to look at the Allocate method. Youâve already seen it. Itâs in listing 8.4. When you look at that code, you can forget about the WillAccept method. Looking at Allocate is another zoom-in operation in the fractal architecture. Remember that what you see is all there is[51]. What you need to know should be right there in the code.
The Allocate method does a good job of that. It activates six objects. Apart from the object property Tables, all are declared and used inside the method. This means that you donât need to keep in your head any other context that impacts how the method works. It fits in your brain.
It still delegates some of its implementation to other objects. It calls Reserve on table, and the Fits method makes another appearance. If youâre curious about the Fits method, you could go and look at that as well. You can see it in listing 8.14.
internal bool Fits(int quantity) { return quantity <= Seats; }
Listing 8.14: The Fits method. Seats is a read-only int property. (Restaurant/62f3a56/Restaurant.RestApi/Table.cs)
This is not even close to the limits of our brainsâ capacity, but it still abstracts two chunks (Seats and quantity) into one. It represents yet another zoom-in operation in the fractal architecture. When you read the source code of Fits, you only need to keep track of Seats and quantity. You donât have to care about the code that calls the Fits method in order to understand how it works. It fits in your brain.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(25281)
Hello! Python by Anthony Briggs(24334)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(23419)
Kotlin in Action by Dmitry Jemerov(22503)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(21959)
Dependency Injection in .NET by Mark Seemann(21837)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(20704)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(19517)
Grails in Action by Glen Smith Peter Ledbrook(18594)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17028)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(15836)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(13683)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(11850)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11151)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10621)
Hit Refresh by Satya Nadella(9185)
The Kubernetes Operator Framework Book by Michael Dame(8562)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8400)
Robo-Advisor with Python by Aki Ranin(8357)